package com.lanchewuyou.user.driver.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.Utils.TUtils;
import com.lanchewuyou.user.driver.lipeiui.LiPeiMainActivity;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.LocationApplication;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 快速登录界面，当用户登录资料失效时进入此界面
 * 将获取到的司机信息保存到本地文件中作为缓存
 */
public class SigninActivity extends AppCompatActivity {
    private MaterialEditText signinName;
    private MaterialEditText signinPassword;
    private Button signinBtn;
    private BDLocation location;
    private Toolbar mToolbar;
    private LocationClient mLocationClient;
    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    private String tempcoor = "bd09ll";
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);
        initViews();
        mLocationClient = ((LocationApplication) getApplication()).mLocationClient;
        initLocation();
        mLocationClient.start();//开启定位
    }

    private void initViews() {
        signinName = (MaterialEditText) findViewById(R.id.signinname);
        signinPassword = (MaterialEditText) findViewById(R.id.signinpassword);
        signinBtn = (Button) findViewById(R.id.signinbtn);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("快速登录");
        setSupportActionBar(mToolbar);
        signinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();
                OkHttpClientManager.getAsyn(Config.SIGN_URL + "&mobile=" + signinName.getText().toString() + "&password=" + signinPassword.getText().toString(),
                        new OkHttpClientManager.StringCallback() {
                            @Override
                            public void onFailure(Request request, IOException e) {
                                progressDialog.dismiss();
                                TUtils.showShort(SigninActivity.this, "服务器出了点问题");
                            }

                            @Override
                            public void onResponse(String response) {
                                Log.d("WELCOME", response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    String status = jsonObject.getString("status");
                                    if (status.equals("1")) {
                                        JSONObject driverinfo = jsonObject.getJSONObject("driverinfo");
                                        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sharedPreferences.edit();
                                        editor.putString("mobile", signinName.getText().toString());
                                        editor.putString("password", signinPassword.getText().toString());
                                        editor.putString("driverid", driverinfo.getString("id"));
                                        editor.putString("drivername", driverinfo.getString("name"));
                                        editor.putString("driverpic", driverinfo.getString("pic"));
                                        editor.commit();
                                        Intent intent = new Intent(SigninActivity.this, LiPeiMainActivity.class);
                                        startActivity(intent);
                                        finish();
                                        overridePendingTransition(R.anim.right_in, R.anim.not_move);
                                        progressDialog.dismiss();
                                    } else {
                                        TUtils.showShort(SigninActivity.this, "用户名或密码有误");
                                        progressDialog.dismiss();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    TUtils.showShort(SigninActivity.this, "json error");
                                }
                            }
                        });

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        mLocationClient.stop();
        super.onStop();
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType(tempcoor);//可选，默认gcj02，设置返回的定位结果坐标系，
        option.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mLocationClient.setLocOption(option);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("登录中");
        progressDialog.setMessage("请稍等");
        progressDialog.setCancelable(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    }
}
